package com.jelly.common.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <h4>功能:【java泛型List工具类】【创建人：QIAOLIANG】</h4>
 *
 * @时间:2018/7/5 0005 17:05
 * @备注:<h4></h4>
 */
public class ListUtil {


    public static <T, K> List<K> getFieldValues(List<T> data, String fieldName) {
        // 存储已经有过的值
        List<K> fieldValues = new ArrayList<K>();
        if (data == null) {
            return fieldValues;
        }
        // 取出不重复的值存储
        for (T t : data) {
            Class groupObject = t.getClass();
            try {
                Field field = getClassFieldContainParentClass(groupObject, fieldName);
                if (field != null) {
                    field.setAccessible(true);
                    Object value = field.get(t);
                    if (value != null && !fieldValues.contains(value)) {
                        fieldValues.add((K) value);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fieldValues;
    }

    private static Field getClassFieldContainParentClass(Class obj, String fieldName) {
        try {
            if (obj != null) {
                Field field = obj.getDeclaredField(fieldName);
                return field;
            }
            return null;
        } catch (Exception e) {
            Class parentObj = obj.getSuperclass();
            return getClassFieldContainParentClass(parentObj, fieldName);
        }
    }

    public static boolean isNotList(List list) {

        if(null !=list && 0 < list.size()){
            return true;
        }

        return false;

    }

    public static boolean isNotSet(Set set) {

        if(null !=set && 0 < set.size()){
            return true;
        }

        return false;

    }

}